<?php

namespace app\common\model;

use think\Model;
use Carbon\Carbon;
use think\Loader;
use think\Exception;
use app\common\traits\model\TraitModel;

class Camps extends Model
{
    protected $table = 'camps';

    protected $type  = [
        'peak_date' => 'array'
    ];

    use TraitModel;

    public function carvans(){
        return $this->hasMany('Carvans','services');
    }

    public function filiale()
    {
        return $this->belongsTo('\\app\\supermanager\\model\\Filiales', 'branch_id');
    }

    public function managers()
    {
        return $this->hasMany('\\app\\manager\\model\\CampManagers','camp_id');
    }

    public function getCampInfo($camp_id, $field = null)
    {
    	if ($campInfo = $this->get(['id' => $camp_id])) {
    		if ($field) {
	    		if (isset($campInfo[$field])) {
	    			return $campInfo->$field;
	    		}
	    		throw new Exception("Field ( $field ) does not exist", 1);    		    		    		
	    	}
	    	return $campInfo;
    	}
    	throw new Exception("Camp information does not exist", 1);
    }
    /**
     * 获取营地旺季时间段
     * @param  [type] $camp_id [description]
     * @return [type]          [description]
     */
    public function getPeakDate($camp_id)
    {
    	return $this->getCampInfo($camp_id, 'peak_date');
    }

    /**
     * 设置营地旺季时间
     * @param [type]  $camp_id   [description]
     * @param [type]  $peak_date [description]
     * @param boolean $safe      [description]
     */
    public function setPeakDate($camp_id, $peak_date, $safe = true)
    {
    	if ($safe) {
	    	if (key($peak_date) === 0) {
	    		foreach ($peak_date as &$p_d) {
	    			$p_d['end_date']   = Carbon::createFromFormat('Y-n-j', $p_d['end_date'])->toDateString();
	    			$p_d['start_date'] = Carbon::createFromFormat('Y-n-j', $p_d['start_date'])->toDateString();
	    		}
	    	}else{
	    		$peak_date['end_date']   = Carbon::createFromFormat('Y-n-j', $peak_date['end_date'])->toDateString();
	    		$peak_date['start_date'] = Carbon::createFromFormat('Y-n-j', $peak_date['start_date'])->toDateString();
	    	}
    	}
    	return $this->update(['peak_date' => $peak_date], ['id' => $camp_id], true);
    }

    public function changeRentMoneyBase($camp_id)
    {
        $this->startTrans();
        try {
            $camp_info        = $this->get($camp_id);
            $single_day_price = $camp_info->rent_money_base;
            $single_day_coin  = $camp_info->travel_money_base;

            $calendars = Loader::model('Calendar')->all(['camps_id' => $camp_info->id, 'datetimes' => ['>=', Carbon::now()->toDateString()]]);

            foreach ($calendars as $calendar) {
                $holiday_coefficient        = \app\common\model\Holidays::$is_holiday[$calendar->is_holiday];
                $calendar->single_day_price = $single_day_price * $holiday_coefficient;
                $calendar->single_day_coin  = $single_day_coin * $holiday_coefficient;
                $calendar->save();
            }
            $this->commit();

        } catch (\Exception $e) {
            $this->rollback();   
            throw $e;         
        }
            
    }
}
